package ta.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "/Filter2", urlPatterns = { "/admin/*"})
public class AuthFilter extends HttpFilter implements Filter {

	private static final long serialVersionUID = 1L;

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		Object obj = req.getSession().getAttribute("loginame");
		if (obj == null) {
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/html; charset=UTF-8");

			res.getWriter().print("<script>alert('请登录后在来访问该页面！')</script>");
			res.setHeader("refresh", "0.1;url=" + req.getContextPath() + "/login.jsp");
		} else {
			chain.doFilter(req, res);
		}
	}
}
